
//#include "head.h"
//#include <stdio.h>
#include <signal.h>

#include <iostream>

#include <workflow/WFFacilities.h>
#include <workflow/WFTaskFactory.h>
#include <workflow/MySQLResult.h>
using std::cout;
using std::endl;
using std::string;

//
//初始化计数器的值为1
static WFFacilities::WaitGroup waitGroup(1);

void sighandler(int num)
{
    printf(">> sig num %d is comming\n", num);
    //调用一次done方法，计数器的值减1
    waitGroup.done();
}

void mysqlCallback(WFMySQLTask * mysqltask)
{
    printf("mysqlCallback is running.\n");
    //0. 对任务的状态进行检测
    int state = mysqltask->get_state();
    int error = mysqltask->get_error();
    if(state != WFT_STATE_SUCCESS) {
        printf("error msg: %s\n", WFGlobal::get_error_string(state, error));
        return;
    }
    //1. 对SQL进行语法检测
    auto resp = mysqltask->get_resp();
    if(resp->get_packet_type() == MYSQL_PACKET_ERROR) {
        printf("ERROR %d: %s\n", resp->get_error_code(),
                                 resp->get_error_msg().c_str());
    }

    //2. 对结果集进行遍历
    protocol::MySQLResultCursor cursor(resp);
    if(cursor.get_cursor_status() == MYSQL_STATUS_OK) {
        //写操作正常执行了
        printf("Query OK, %ld rows affected.\n",
                    cursor.get_affected_rows());
    }
}

void test0()
{
    signal(SIGINT, sighandler);
    
    string url("mysql://root:1234@localhost");
    auto mysqlTask = WFTaskFactory::create_mysql_task(url, 1, mysqlCallback);
    string sql = "INSERT INTO cloudisk.tbl_user_token(user_name, user_token) VALUES('Guanyu', 'token_aaa')";
    mysqlTask->get_req()->set_query(sql);
    mysqlTask->start();

    //当调用wait方法会阻塞当前线程
    waitGroup.wait();

    cout << "test0 exit" << endl;
}


int main()
{
    test0();
    return 0;
}

